using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using AjGenesisStudio.Properties;
using System.IO;

namespace AjGenesisStudio.Commands
{
    class ExecuteCommand : Command
    {
        string build_file;
        string dir_path;

        public override bool Execute()
        {
            if (LookForBuild())
                ExecuteShell("nant.exe", "-buildfile:" + build_file);
            else
                MessageBox.Show("There is no '.build' file in your project directory.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return true;
        }

        private bool LookForBuild()
        {
            string tmp;
            dir_path = Panels.ExplorerPanel.Instance.DirectoryPath;

            if (Directory.Exists(dir_path))
            {
                //.build seleccionado
                tmp = Panels.ExplorerPanel.Instance.SelectedPath;
                if (Path.GetExtension(tmp) == ".build")
                {
                    build_file = tmp;
                    return true;
                }

                tmp = Path.Combine(dir_path, "default.build");
                if (File.Exists(tmp))
                {
                    build_file = tmp;
                    return true;
                }
            }
            return false;
        }

        private void ExecuteShell(string file, string args)
        {
            Global.Instance.StartWork("Executing " + Path.GetFileName(file));
            
            System.Diagnostics.Process proc;

            proc = new System.Diagnostics.Process();
            if (!string.IsNullOrEmpty(dir_path))
                proc.StartInfo.WorkingDirectory = dir_path;
            proc.StartInfo.FileName = file;
            proc.StartInfo.Arguments = args;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.ErrorDialog = false;
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.RedirectStandardOutput = true;
            try
            {
                proc.Start();
                while (!proc.HasExited)
                {
                    string line = proc.StandardOutput.ReadLine();
                    if (!string.IsNullOrEmpty(line))
                        Global.Instance.LogLine(line);
                }
                proc.WaitForExit();
            }
            catch (System.ComponentModel.Win32Exception exwin)
            {
                if (exwin.ErrorCode == -2147467259)
                    MessageBox.Show("Can not find NAnt.exe! Please ensure that NAnt is installed in your system and the nant bin directory is added to the 'Path' system variable.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    MessageBox.Show(exwin.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            Global.Instance.EndWork();
        }
    }
}
